<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item  label="类型">
          <el-select v-model="dataForm.datasourceType" placeholder="请选择">
             <el-option label="MySQL" value="mysql"></el-option>
             <el-option label="MongoDB" value="mongodb"></el-option>
             <el-option label="Redis" value="redis"></el-option>
           </el-select>
      </el-form-item>
      <el-form-item  label="服务器">
          <el-select style="width:350px"  v-model="dataForm.serverId" placeholder="请选择">
             <el-option v-for="( s,index) in servers" :label="s.name+'  【'+s.ipAddr+'】'" :value="s.id"></el-option>
           </el-select>
           <span style="padding-left:30px;">端口：</span>
           <el-input style="width:100px" v-model="dataForm.port" placeholder="端口"></el-input>
      </el-form-item>
      <el-form-item  label="部署路径">
          <el-input v-model="dataForm.deployPath" placeholder="部署路径"></el-input>
      </el-form-item>
      <el-form-item  label="配置文件">
          <el-input v-model="dataForm.configPath" placeholder="配置文件路径"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      return {
        visible: false,
        servers:[],
        dataForm: {
          id: 0,
          name: '',
          status: 1,
          port:0,
          datasourceType: '',
          serverId: '',
          deployPath: '',
          configPath:'',

        },
        dataRule: {
          name: [
            { required: true, message: '服务器名称不能为空', trigger: 'blur' }
          ],

          ipAddr: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl(`/systemconfig/servers/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.servers = data.data
          }
        })
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/datasource/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.server
              }
            })
          }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
                this.$http({
                  url: this.$http.adornUrl(`/datasource/${!this.dataForm.id ? 'save' : 'update'}`),
                  method: 'post',
                  data: this.$http.adornData({
                    'id': this.dataForm.id || undefined,
                    'name': this.dataForm.name,
                   'datasourceType': this.dataForm.datasourceType,
                    'serverId': this.dataForm.serverId,
                    'port': this.dataForm.port,
                    'deployPath': this.dataForm.deployPath,
                    'configPath': this.dataForm.configPath,
                    'status': this.dataForm.status,
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            }
        })
      }
    }
  }
</script>
